*************************************
*Figure 4: Robots and field of study*
*************************************

*Education
use "$final_data_outcomes/czone_Students.dta", clear
merge 1:1 czone year using "$final_data_outcomes/IPEDS_EnrollmentCZ.dta", assert(1 3) nogenerate
merge 1:1 czone year using "$final_data_outcomes/IPEDS_GraduationCZ.dta", assert(1 3) nogenerate
merge 1:1 czone year using "$final_data_outcomes/IPEDS_MajorsCZ.dta", assert(1 3) nogenerate

*Automation
merge 1:1 czone year using "$final_data_automation/czones_ExposureAutomation.dta", assert(1 3) nogenerate

*Imports merge
merge 1:1 czone year using "$final_data_imports/czone_ExposureChina", assert(1 3) nogenerate

*Covariates
merge m:1 czone using "$final_data_covariates/czone1990_Covariates.dta", assert(3) nogenerate
merge m:1 czone using "$final_data_covariates/IPEDS_institutionControlsCZ.dta", assert(1 3) nogenerate

xtset czone year

egen medianAnyAid = median(anyAid), by(statefip)
	replace anyAid = medianAnyAid if mi(anyAid)

*Keep periods of interest
keep if year<=2000

*Create confidence intervals
gen coef =.
gen se =.
gen ci_u =.
gen ci_d =.

gen name = ""

*Regression coefficients
local i = 1

foreach var in EngCom BusEco SocialS PubMil Manuf Natural HealthS HumArts  {
eststo: ivreghdfe d_share`var' (expof_us_adj d_exp_us_stacked = expof_euro7_qo d_exp_iv_stacked) d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)

replace coef = _b[expof_us_adj] in `i'
replace se = _se[expof_us_adj] in `i'
replace ci_u = coef + 1.96*se in `i'
replace ci_d = coef - 1.96*se in `i'

replace name = "`var'" in `i'

local i = `i'+1

}

keep if _n<=8
keep coef* se* ci_* name

gsort -coef

gen id = _n

gen field = ""
replace field = "Computer Science and Engineering" if id==1
replace field = "Business and Economics" if id==2
replace field = "Social Science" if id==3
replace field = "Public and Military" if id==4
replace field = "Natural Science" if id==5
replace field = "Manufacturing" if id==6
replace field = "Arts and Humanities" if id==7
replace field = "Health Science" if id==8

labmask id, values(field)

twoway  (bar coef id, barwidth(0.7) fcolor(navy)  lcolor(black) lwidth(vthin)) ///
		(scatter coef id, color(black) msymbol(diamond) msize(small)) ///
		(rcap ci_u ci_d id, lcolor(black) lwidth(vthin)) ///
		, xscale(r(0.5 8.5)) ylabel(-2.5(.5)1.5, angle(horiz) labsize(medium) grid gmin gmax format(%5.2f)) yline(0, lcolor(black) lwidth(vthin) lpattern(shortdash)) xtitle("")  ///
		legend(off) ysize(2.5) ytitle("US robot exposure" "point estimate", size(medium)) ///
		xscale(noline) xlabel(1(1)8, valuelabels labsize(medium) angle(45) noticks axis(1)) ///
		graphregion(color(white) fcolor(white) margin(r+30 l+10 t+10))
		graph export "$figures_main/Figure 3.png", replace
	
		
		
